package com.wrench.elevator.tida.service;

import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.ui.ModelMap;

import com.alibaba.fastjson.JSON;
import com.wrench.elevator.tida.constant.Result;
import com.wrench.elevator.tida.dao.DTGLDao;
import com.wrench.elevator.tida.domain.Dianti;
import com.wrench.elevator.tida.domain.Page;
import com.wrench.elevator.tida.exception.WrenchException;
import com.wrench.elevator.tida.util.WrenchUtil;

@Component
public class DTGLService {
	@Resource
	private DTGLDao dao;

	private static final Logger logger = LoggerFactory
			.getLogger(DTGLService.class);

	public void addZhuangDanyuan(String building_id, String zhuang_name,
			String danyuan_name, ModelMap map) throws WrenchException {
		if (WrenchUtil.hasNull(building_id, zhuang_name, danyuan_name)) {
			logger.error(
					"==> param is not found , building_id = {} , zhuang_name = {} , danyuan_name = {} ",
					building_id, zhuang_name, danyuan_name);
			throw new WrenchException(Result.PARAM_IS_NOT_FOUND_CODE,
					Result.PARAM_IS_NOT_FOUND_MESSAGE);
		}
		WrenchUtil.getParamMap(map, "building_id", building_id, "zhuang_name",
				zhuang_name, "danyuan_name", danyuan_name);
		try {
			dao.addZhuangDanyuan(map);
		} catch (Exception e) {
			logger.error(
					"==> dao.addZhuangDanyuan(map) execute error , map = {} , error = {} ",
					JSON.toJSONString(map), e);
			throw new WrenchException(Result.DB_ERROR_CODE,
					Result.DB_ERROR_MESSAGE);
		}

	}

	public int addElevatorAndCamera(String building_name,
			String zhuang_danyuan, String elevator_num, String camera_num)
			throws WrenchException {
		if (WrenchUtil.hasNull(building_name, zhuang_danyuan, elevator_num,
				camera_num)) {
			logger.error(
					"==> param is not found , building_name = {} , zhaung_danyuan = {} , elevator_num = {} , camera_num = {} ",
					building_name, zhuang_danyuan, elevator_num, camera_num);
			throw new WrenchException(Result.PARAM_IS_NOT_FOUND_CODE,
					Result.PARAM_IS_NOT_FOUND_MESSAGE);
		}
		int commit = -1;
		try {
			commit = dao.addElevatorAndCamera(building_name, zhuang_danyuan,
					elevator_num, camera_num);
		} catch (Exception e) {
			logger.error(
					"==> commit = dao.addElevatorAndCamera(building_name, zhuang_danyuan, elevator_num, camera_num) execute error , building_name = {} , zhaung_danyuan = {} , elevator_num = {} , camera_num = {} , error = {} ",
					building_name, zhuang_danyuan, elevator_num, camera_num, e);
			throw new WrenchException(Result.DB_ERROR_CODE,
					Result.DB_ERROR_MESSAGE);
		}
		if (commit <= 0) {
			logger.error(
					"==> commit = dao.addElevatorAndCamera(building_name, zhuang_danyuan, elevator_num, camera_num) execute error , building_name = {} , zhaung_danyuan = {} , elevator_num = {} , camera_num = {} , commit = {} ",
					building_name, zhuang_danyuan, elevator_num, camera_num,
					commit);
			throw new WrenchException(Result.DEL_DT_FAIL_CODE,
					Result.DEL_DT_FAIL_MESSAGE);
		}
		return commit;

	}

	public void addElevator(String building_name, String zhuang_danyuan,
			String elevator_num, ModelMap map) throws WrenchException {

		if (WrenchUtil.hasNull(building_name, zhuang_danyuan, elevator_num)) {
			logger.error(
					"==> param is not found , building_name = {} , zhaung_danyuan = {} , elevator_num = {} ",
					building_name, zhuang_danyuan, elevator_num);
			throw new WrenchException(Result.PARAM_IS_NOT_FOUND_CODE,
					Result.PARAM_IS_NOT_FOUND_MESSAGE);
		}

		WrenchUtil.getParamMap(map, "building_name", building_name,
				"zhuang_danyuan", zhuang_danyuan, "elevator_num", elevator_num);

		try {
			dao.addElevator(map);
		} catch (Exception e) {
			logger.error(
					"==> dao.addElevator(map) execute error, map = {} , error = {} ",
					JSON.toJSONString(map), e);
			throw new WrenchException(Result.DB_ERROR_CODE,
					Result.DB_ERROR_MESSAGE);
		}
		logger.debug("==> dao.addElevator(map) execute success, map = {}  ",
				JSON.toJSONString(map));

	}

	public void addCamera(String building_name, String zhuang_danyuan,
			String camera_num, ModelMap map) throws WrenchException {

		if (WrenchUtil.hasNull(building_name, zhuang_danyuan, camera_num)) {
			logger.error(
					"==> param is not found , building_name = {} , zhaung_danyuan = {} , camera_num = {} ",
					building_name, camera_num, camera_num);
			throw new WrenchException(Result.PARAM_IS_NOT_FOUND_CODE,
					Result.PARAM_IS_NOT_FOUND_MESSAGE);
		}

		WrenchUtil.getParamMap(map, "building_name", building_name,
				"zhuang_danyuan", zhuang_danyuan, "camera_num", camera_num);

		try {
			dao.addCamera(map);
		} catch (Exception e) {
			logger.error(
					"==> dao.addCamera(map) execute error, map = {} , error = {} ",
					JSON.toJSONString(map), e);
			throw new WrenchException(Result.DB_ERROR_CODE,
					Result.DB_ERROR_MESSAGE);
		}
		logger.debug("==> dao.addCamera(map) execute success, map = {}  ",
				JSON.toJSONString(map));

	}

	public int delDT(String elevator_id) throws WrenchException {
		if (WrenchUtil.hasNull(elevator_id)) {
			logger.error("==> param is not found , elevator_id = {} ",
					elevator_id);
			throw new WrenchException(Result.PARAM_IS_NOT_FOUND_CODE,
					Result.PARAM_IS_NOT_FOUND_MESSAGE);
		}

		if (!WrenchUtil.isInt(elevator_id)) {
			logger.error("==>  string is not int type , elevator_id = {} ",
					elevator_id);
			throw new WrenchException(Result.STRING_IS_NOT_INT_TYPE_CODE,
					Result.STRING_IS_NOT_INT_TYPE_MESSAGE);
		}

		int commit = -1;
		try {
			commit = dao.delDT(Integer.valueOf(elevator_id));
		} catch (Exception e) {
			logger.error(
					"==> commit=dao.delDT(Integer.valueOf(elevator_id)) execute error , elevator_id = {} , error = {} ",
					elevator_id, e);
			throw new WrenchException(Result.DB_ERROR_CODE,
					Result.DB_ERROR_MESSAGE);
		}
		if (commit <= 0) {
			logger.error(
					"==> commit=dao.delDT(Integer.valueOf(elevator_id)) execute success , but commit <=0 , elevator_id = {} , commit = {} ",
					elevator_id, commit);
			throw new WrenchException(Result.DEL_DT_FAIL_CODE,
					Result.DEL_DT_FAIL_MESSAGE);
		}
		return commit;
	}

	public Page queryDT(String name, String current_page, String limit)
			throws WrenchException {
		if (WrenchUtil.hasNull(name, current_page, limit)) {
			logger.error(
					"==> param is not found , name = {} , current_page = {} , limit = {} ",
					name, current_page, limit);
			throw new WrenchException(Result.PARAM_IS_NOT_FOUND_CODE,
					Result.PARAM_IS_NOT_FOUND_MESSAGE);
		}

		if (!WrenchUtil.areInt(current_page, limit)) {
			logger.error(
					"==> string is not int type , current_page = {} , limit = {} ",
					current_page, limit);
			throw new WrenchException(Result.STRING_IS_NOT_INT_TYPE_CODE,
					Result.STRING_IS_NOT_INT_TYPE_MESSAGE);
		}

		List<Dianti> diantiList = null;
		try {
			diantiList = dao.queryDT(name, (Integer.valueOf(current_page) - 1)
					* Integer.valueOf(limit), Integer.valueOf(limit));
		} catch (Exception e) {
			logger.error(
					"==> diantiList=dao.queryDT(name, (Integer.valueOf(current_page)-1)*Integer.valueOf(limit), Integer.valueOf(limit)) execute error , name = {} , current_page = {} , limit = {} , error = {} ",
					name, current_page, limit, e);
			throw new WrenchException(Result.DB_ERROR_CODE,
					Result.DB_ERROR_MESSAGE);
		}

		int total = -1;
		try {
			total = dao.queryDTCount(name);
		} catch (Exception e) {
			logger.error(
					"==> total=dao.queryDTCount(name) execute error , name = {} , error = {} ",
					name, e);
			throw new WrenchException(Result.DB_ERROR_CODE,
					Result.DB_ERROR_MESSAGE);
		}

		Page page = new Page();
		page.setCurrent_page(Integer.valueOf(current_page));
		page.setLimit(Integer.valueOf(limit));
		page.setTotal(total);
		page.setList(diantiList);
		return page;
	}

	public Page queryDT2(String building_name, String zhuang_danyuan,
			String current_page, String limit) throws WrenchException {
		if (WrenchUtil.hasNull(building_name, zhuang_danyuan, current_page,
				limit)) {
			logger.error(
					"==> param is not found , building_name = {} , zhuang_danyuan = {} , current_page = {} , limit = {} ",
					building_name, zhuang_danyuan, current_page, limit);
			throw new WrenchException(Result.PARAM_IS_NOT_FOUND_CODE,
					Result.PARAM_IS_NOT_FOUND_MESSAGE);
		}

		if (!WrenchUtil.areInt(current_page, limit)) {
			logger.error(
					"==> string is not int type , current_page = {} , limit = {} ",
					current_page, limit);
			throw new WrenchException(Result.STRING_IS_NOT_INT_TYPE_CODE,
					Result.STRING_IS_NOT_INT_TYPE_MESSAGE);
		}

		List<Dianti> diantiList = null;
		try {
			diantiList = dao.queryDTByBuildingNameAndZhuangdanyuan(
					building_name,
					zhuang_danyuan,
					(Integer.valueOf(current_page) - 1)
							* Integer.valueOf(limit), Integer.valueOf(limit));
		} catch (Exception e) {
			logger.error(
					"==>  dao.queryDTByBuildingNameAndZhuangdanyuan(building_name,zhuang_danyuan, (Integer.valueOf(current_page) - 1) * Integer.valueOf(limit), Integer.valueOf(limit)) execute error , building_name  = {} , zhuang_danyuan = {}  current_page = {} , limit = {} , error = {} ",
					building_name, zhuang_danyuan, current_page, limit, e);
			throw new WrenchException(Result.DB_ERROR_CODE,
					Result.DB_ERROR_MESSAGE);
		}

		int total = -1;
		try {
			total = dao.queryDTByBuildingNameAndZhuangdanyuanCount(
					building_name, zhuang_danyuan);
		} catch (Exception e) {
			logger.error(
					"==> total=queryDTByBuildingNameAndZhuangdanyuanCount(building_name,zhuang_danyuan) execute error , building_name = {} , zhuang_danyuan = {} , error = {} ",
					building_name, zhuang_danyuan, e);
			throw new WrenchException(Result.DB_ERROR_CODE,
					Result.DB_ERROR_MESSAGE);
		}

		Page page = new Page();
		page.setCurrent_page(Integer.valueOf(current_page));
		page.setLimit(Integer.valueOf(limit));
		page.setTotal(total);
		page.setList(diantiList);
		return page;
	}

}
